package com.chengyu.core.service.goods.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chengyu.core.dao.GoodsSpecDao;
import com.chengyu.core.domain.result.ShopAccountDetails;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.exception.CommonException;
import com.chengyu.core.model.GoodsSpec;
import com.chengyu.core.model.UmsShopAccount;
import com.chengyu.core.request.GoodsSpecRequest;
import com.chengyu.core.request.GoodsSpecSearchRequest;
import com.chengyu.core.request.PageParamRequest;
import com.chengyu.core.service.goods.GoodsSpecService;
import com.chengyu.core.util.SecurityUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * GoodsSpecServiceImpl 接口实现
 */
@Service
public class GoodSpecServiceImpl extends ServiceImpl<GoodsSpecDao, GoodsSpec> implements GoodsSpecService {

    @Resource
    private GoodsSpecDao dao;


    /**
    * 列表
    * @param request 请求参数
    * @param pageParamRequest 分页类参数
    * @return PageInfo<GoodsSpec>
    */
    @Override
    public PageInfo<GoodsSpec> getList(GoodsSpecSearchRequest request, PageParamRequest pageParamRequest) {

        UmsShopAccount umsShopAccount = SecurityUtil.getLoginUserVo().getShopAccount();
        Page<GoodsSpec> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<GoodsSpec> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GoodsSpec::getMerId, umsShopAccount.getShopId());
        if (StrUtil.isNotBlank(request.getKeywords())) {
            lambdaQueryWrapper.like(GoodsSpec::getRuleName, request.getKeywords());
        }
        lambdaQueryWrapper.orderByDesc(GoodsSpec::getId);
        List<GoodsSpec> list = dao.selectList(lambdaQueryWrapper);
        return CommonPage.copyPageInfo(page, list);
    }

    /**
     * 新增商品规格
     * @param GoodsSpecRequest 规格参数
     * @return 新增结果
     */
    @Override
    public boolean save(GoodsSpecRequest GoodsSpecRequest) {
        UmsShopAccount umsShopAccount = SecurityUtil.getLoginUserVo().getShopAccount();
        if (existRuleName(GoodsSpecRequest.getRuleName(), umsShopAccount.getShopId())) {
            throw new CommonException("此规格值已经存在");
        }
        GoodsSpec GoodsSpec = new GoodsSpec();
        BeanUtils.copyProperties(GoodsSpecRequest, GoodsSpec);
        GoodsSpec.setMerId(umsShopAccount.getShopId());
        return save(GoodsSpec);
    }

    /**
     * 规格名是否存在
     * @param ruleName 规格名
     */
    private Boolean existRuleName(String ruleName, Integer merId) {
        LambdaQueryWrapper<GoodsSpec> lqw = Wrappers.lambdaQuery();
        lqw.select(GoodsSpec::getId);
        lqw.eq(GoodsSpec::getRuleName, ruleName);
        lqw.eq(GoodsSpec::getMerId, merId);
        lqw.last(" limit 1");
        GoodsSpec productRule = dao.selectOne(lqw);
        return ObjectUtil.isNotNull(productRule);
    }

    /**
     * 修改规格
     * @param GoodsSpecRequest 规格参数
     * @return Boolean
     */
    @Override
    public Boolean updateRule(GoodsSpecRequest GoodsSpecRequest) {
        if (ObjectUtil.isNull(GoodsSpecRequest.getId())) {
            throw new CommonException("请先选择规格");
        }
        GoodsSpec GoodsSpec = new GoodsSpec();
        BeanUtils.copyProperties(GoodsSpecRequest, GoodsSpec);
        return updateById(GoodsSpec);
    }

}

